package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.common.entity.admin.SysMenu;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.response.Response;
import com.iot08.admin.service.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @version 1.0
 * @Description: 菜单管理控制器
 * @author: Rock
 * @time: 2020/8/12 15:14
 */
@Controller
@RequestMapping("menu")
public class MenuController extends BaseController {

    @Resource
    private MenuService menuService;

    /**
     * @Description: 菜单列表
     * @author: Rock
     * @time: 2020/8/12 18:33
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(String menuId) {
        ModelAndView mv = new ModelAndView();
        List<SysMenu> menus = menuService.listAll();
        mv.addObject("menuList", menus);
        mv.addObject("butSet", getMyBut(menuId));
        mv.addObject("menuId", menuId);
        mv.setViewName("menu/menu_list");
        return mv;
    }

    /**
     * @Description:
     * @author: Rock
     * @time: 2020/8/13 11:21
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "/sub")
    public Response<Object> getSub(String id, HttpServletResponse response) throws Exception {
        List<SysMenu> subMenu = menuService.listByPid(id);
        return Response.successResponse(subMenu);
    }

    /**
     * @Description: 添加菜单
     * @author: Rock
     * @time: 2020/8/12 18:34
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        List<SysMenu> menus = menuService.listNotBut();
        mv.addObject("menuList", menus);
        mv.setViewName("menu/menu_add");
        return mv;
    }

    /**
     * @Description: 添加菜单
     * @author: Rock
     * @time: 2020/8/13 12:05
     * @version 1.0
     */
    @RequestMapping("add")
    public ModelAndView add(SysMenu menu) {
        ModelAndView mv = new ModelAndView();
        SysUser user = this.getSessionUser();
        if (user != null) {
            menu.setCreator(user.getId());
            menuService.save(menu);
            mv.addObject("msg", "success");
        } else {
            mv.addObject("msg", "failed");
        }
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @Description: 更新页面
     * @author: Rock
     * @time: 2020/8/13 12:11
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        List<SysMenu> menus = menuService.listNotBut();
        mv.addObject("menu", menuService.getById(id));
        mv.addObject("menuList", menus);
        mv.setViewName("menu/menu_edit");
        return mv;
    }

    /**
     * @Description: 保存更新
     * @author: Rock
     * @time: 2020/8/13 14:25
     * @version 1.0
     */
    @RequestMapping("edit")
    public ModelAndView edit(SysMenu menu) {
        ModelAndView mv = new ModelAndView();
        menuService.update(menu);
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @Description: 删除菜单
     * @author: Rock
     * @time: 2020/8/13 18:14
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "del")
    public Response<Object> del(String id) {
        return menuService.updateDeleted(id);
    }

}
